//
//  YHEmotionCell.swift
//  EmotionKeyboard
//
//  Created by 昱含 on 16/5/30.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

class YHEmotionCell: UICollectionViewCell {
    
    var emotions: [YHEmotions]? {
        didSet {
            //绑定数据
            //获取所有的按钮
            //给所有的按钮设置模型数据
            for (index,emotion) in emotions!.enumerate() {
                //获取每一个模型
                let btn = buttons[index]
                btn.emotion = emotion
            }
        }
    }
    
    private lazy var buttons: [YHEmotionButton] = [YHEmotionButton]()
    
    var indexPath: NSIndexPath? {
        didSet{
            testLable.text = "第\(indexPath?.section ?? 0)组,第\(indexPath?.item ?? 0)cell"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
//        backgroundColor = UIColor.redColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
//        contentView.addSubview(testLable)
//        testLable.snp_makeConstraints { (make) in
//        make.center.equalTo(contentView.snp_center)
//        }
        for i in 0..<21 {
            let margin: CGFloat = 5
            let bottomMargin: CGFloat = 30
            let w = (UIScreen.mainScreen().bounds.width - 2 * margin) / CGFloat(EmotionColCount)
            let h = (self.bounds.height - bottomMargin) / CGFloat(EmotionRowCount)
            let btn = YHEmotionButton()
            btn.titleLabel?.font = UIFont.systemFontOfSize(32)
            btn.addTarget(self, action: #selector(YHEmotionCell.btnDidClick(_:)), forControlEvents: .TouchUpInside)
            let row = i / EmotionColCount
            let col = i % EmotionColCount
            let x: CGFloat = margin + CGFloat(col) * w
            let y: CGFloat = CGFloat(row) * h
            btn.frame = CGRect(x: x, y: y, width: w, height: h)
//            btn.setTitle("dddd", forState: .Normal)
            btn.titleLabel?.font = UIFont.systemFontOfSize(32)
//            btn.setTitleColor(UIColor.redColor(), forState: .Normal)
            btn.backgroundColor = randomColor()
            self.contentView.addSubview(btn)
            buttons.append(btn)
         
        }
        
        
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(YHEmotionCell.longPressFire(_:)))
        self.contentView.addGestureRecognizer(longPress)
    }
    
    @objc private func btnDidClick(btn: YHEmotionButton) {
        NSNotificationCenter.defaultCenter().postNotificationName(EmotionDidClickNotification, object: btn.emotion)
    }
    
    @objc private func longPressFire(ges: UILongPressGestureRecognizer){
        let point = ges.locationInView(contentView)
       
        guard let btn = buttonWithPoint(point) else{
            popView.dismissAfter()
            return
        }
        //        print(btn?.emotion?.chs)
        
        if btn.emotion!.isEmpty || btn.emotion!.isDelete {
            popView.dismissAfter()
            return
        }
        switch ges.state {
        case .Began,.Changed:
            
            popView.show(btn)
            
        default:
            
            popView.dismissAfter()
        }
        
        
    }
    
    private func buttonWithPoint(point: CGPoint) -> YHEmotionButton? {
        for btn in buttons {
            if btn.frame.contains(point) {
                return btn
            }
            
        }
        //找不到按钮
        return nil
    }
    
    
    
    private lazy var popView: YHEmotionPopView = {
        let pop = YHEmotionPopView.loadPopView()
        return pop
    }()
    
    private lazy var testLable: UILabel = {
        let test = UILabel()
        test.font = UIFont.systemFontOfSize(15)
        test.sizeToFit()
        return test
    }()
}
